<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use app\library\Flag;
use think\Model;
use think\Db;

class NoteReply extends Model
{
    protected $table = 'note_reply';

    protected $pk = 'id';

    /**
     * 回复帖子
     *
     */
    public function addNoteReply($arrInput)
    {
        if (!empty($arrInput['extend_info'])) {
            if (is_array($arrInput['extend_info'])) {
                $arrInput['extend_info'] = json_encode($arrInput['extend_info']);
            }
        } else {
            $arrInput['extend_info'] = '[]';
        }

        unset($arrInput[$this->pk]);

        $this->data($arrInput);
        $this->save();
        return $this->data[$this->pk];
    }

    /**
     * 删除帖子回复
     *
     */
    public function delNoteReply($id)
    {

        if (empty($arrInput['id'])) {
            return false;
        }

        $conds = [
            'id' => ['=', $id]
        ];

        $data = [
            'status' => 1,
            'update_time' => time(),
        ];

        return Db::table($this->table)->where($conds)->update($data);
    }

    /**
     * 删除帖子回复
     *
     */
    public function clearNoteReply($userid)
    {

        if (empty($arrInput['id'])) {
            return false;
        }

        $conds = [
            'note_userid' => ['=', $userid],
            'status' => ['=', 0],
        ];

        $data = [
            'status' => 1,
            'update_time' => time(),
        ];

        return Db::table($this->table)->where($conds)->update($data);
    }

    /**
     * 获取回复列表
     *
     */
    public function getNoteReply($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    /**
     * 获取回复数
     *
     */
    public function getNoteReplyCount($noteids = [])
    {
        if (empty($noteids)) {
            return 0;
        }
        $sql = sprintf('select note_id,count(*) as cnt from %s where note_id in (%s) and is_delete=0 group by note_id', $this->table, implode(',', $noteids));
        return Db::query($sql);
    }

    /**
     * 获取某人的回帖数
     *
     */
    public function getNoteReplyCountByUserid($userid, $date = 0)
    {
        if (empty($userid)) {
            return 0;
        }
        $arrCond = [
            'userid' => ['=', $userid],
            'is_delete' => ['=', Flag::CONST_NOTEREPLY_DELETE_NOT],
        ];

        if ($date) {
            $arrCond['date'] = ['=', $date];
        }

        return $this->where($arrCond)->count();
    }

    /**
     * 获取帖子回复数
     *
     */
    public function getNoteReplyCountByNoteid($noteid, $date = 0)
    {
        if (empty($noteid)) {
            return 0;
        }
        $arrCond = [
            'note_id' => ['=', $noteid],
            'is_delete' => ['=', Flag::CONST_NOTEREPLY_DELETE_NOT],
        ];

        if ($date) {
            $arrCond['date'] = ['=', $date];
        }

        return $this->where($arrCond)->count();
    }

    /**
     * 根据条件获取回帖数
     *
     */
    public function getNoteReplyCountByConds($arrCond)
    {
        return Db::table($this->table)->where($arrCond)->count();
    }

}
